package cn.zn.controller.system;

import cn.zn.base.annotation.Log;
import cn.zn.base.bean.dto.ResultData;
import cn.zn.base.common.constant.Constants;
import cn.zn.base.common.enums.BusinessType;
import cn.zn.base.common.utils.ResultUtil;
import cn.zn.user.entity.SysUser;
import cn.zn.user.entity.SysUserJz;
import cn.zn.user.entity.SysUserRole;
import cn.zn.user.service.SysUserJzService;
import cn.zn.user.service.SysUserRoleService;
import cn.zn.user.service.SysUserService;
import cn.zn.user.vo.SysUserEditVo;
import cn.zn.user.vo.SysUserJzEditVo;
import cn.zn.user.vo.SysUserJzPageVo;
import cn.zn.user.vo.SysUserPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息
 *
 * @author zn-admin
 */
@RestController
@RequestMapping("/system/user")
public class SysUserController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysUserRoleService userRoleService;

    @Autowired
    private SysUserJzService userJzService;

    /**
     * 获取用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public ResultData list(SysUserPageVo vo) {
        Object result = userService.selectUserList(vo);
        return ResultUtil.success(result);
    }

    /**
     * 根据用户编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:info')")
    @GetMapping(value = "/{userId}")
    public ResultData getInfo(@PathVariable(value = "userId") String userId) {
        SysUser user = userService.getById(userId);
        List<SysUserRole> roles = userRoleService.selectUserRoleList(userId);
        Map<String, Object> result = new HashMap<>();
        result.put("user", user);
        result.put("roles", roles);
        return ResultUtil.success(result);
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/{userId}")
    public ResultData edit(@PathVariable(value = "userId") String userId, @RequestBody @Validated SysUserEditVo vo) {
        userService.updateUserRoles(userId, vo);
        return ResultUtil.success();
    }

    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('system:user:resetPwd')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd/{userId}")
    public ResultData resetPwd(@PathVariable(value = "userId") String userId) {
        userService.resetUserPwd(userId, Constants.DEFAULT_PASSWORD);
        return ResultUtil.success();
    }

    @GetMapping("/role/{userId}")
    public ResultData getUserRoles(@PathVariable(value = "userId") String userId) {
        return ResultUtil.success(userRoleService.selectUserRoleList(userId));
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/role/{userId}")
    public ResultData updateUserRoles(@PathVariable(value = "userId") String userId, @RequestBody SysUserEditVo vo) {
        userService.updateUserRoles(userId, vo);
        return ResultUtil.success();
    }

    /**
     * 兼职
     */
    @PreAuthorize("@ss.hasPermi('system:user:partTime')")
    @GetMapping(value = "/jzList")
    public ResultData jzList(SysUserJzPageVo vo) {
        List<SysUserJz> userJzs = userJzService.selectUserJzList(vo);
        return ResultUtil.success(userJzs);
    }

    /**
     * 设置兼职
     *
     * @param vo 兼职
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:user:partTime')")
    @Log(title = "用户兼职", businessType = BusinessType.INSERT)
    @PostMapping(value = "/setJz")
    public ResultData setJz(@RequestBody SysUserJzEditVo vo) {
        userJzService.insertUserJz(vo);
        return ResultUtil.success();
    }

}
